package v1

import (
	"strconv"

	"gitee.com/cyanbleeze/go-image-management/internal/service"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
	"gitee.com/cyanbleeze/go-image-management/pkg/errcode"
	"github.com/gin-gonic/gin"
)

type Public struct {
}

func NewPublic() Public {
	return Public{}
}

func (p Public) List(ctx *gin.Context) {
	res := app.NewResponse(ctx)
	pager := app.Pager{
		Page:     app.GetPage(ctx),
		PageSize: app.GetPageSize(ctx),
	}

	svc := service.New(ctx)
	detailList, count, err := svc.GetPublicPictureList(&pager)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorGetDetailListFail.WithDetails(err.Error()))
		return
	}
	res.ToResponseList(detailList, count)
}
func (p Public) Update(ctx *gin.Context) {
	res := app.NewResponse(ctx)
	id, IDErr := strconv.Atoi(ctx.Param("id"))
	if IDErr != nil {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(IDErr.Error()))
		return
	}
	params := service.TogglePublicRequest{ID: uint(id)}
	valid, errs := app.BindAndValid(ctx, &params)
	if !valid {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}
	svc := service.New(ctx)
	err := svc.TogglePublic(&params)
	if err != nil {
		res.ToErrorResponse(errcode.ErrorUpdateDetailFail.WithDetails(err.Error()))
		return
	}
	res.ToResponse(gin.H{})
}
